package com.lingnan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lingnan.mapper.ActiveMapper;
import com.lingnan.mapper.AdminMapper;
import com.lingnan.pojo.Active;
import com.lingnan.pojo.Admin;
import com.lingnan.service.AdminService;
import com.lingnan.util.HttpRequestUtil;
import com.lingnan.util.MD5Utils;
import com.lingnan.util.ResultGenerator;
import com.lingnan.vo.ActiveVo;
import net.minidev.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author 梁兴达
 * @Date 2022/3/12 17:14:32
 * @Version 1.0
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {

    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private ActiveMapper activeMapper;

    @Override
    public Admin login(Admin admin) {
        QueryWrapper<Admin> wrapper = new QueryWrapper<>();
        wrapper.eq("job_id",admin.getJobId());
        wrapper.eq("password",admin.getPassword());

        Admin one = getOne(wrapper);
        if (one != null){
            return admin;
        }
        return null;
    }

    @Override
    public boolean registerAdmin(Admin admin) {
        Admin admin1 = new Admin();
        admin1.setJobId(admin.getJobId());
        admin1.setLevel(admin.getLevel());
        admin1.setName(admin.getName());
        admin1.setPassword(MD5Utils.MD5Encode(admin.getPassword(),"UTF-8"));
        admin1.setPhone(admin.getPhone());
        int insert = adminMapper.insert(admin1);
        if (insert>0){
            return true;
        }
        return false;
    }

    @Override
    public Active deleteActice(ActiveVo activeVo) {
        Active active = activeMapper.selectById(activeVo.getId());
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id",activeVo.getId());
        int delete = activeMapper.delete(queryWrapper);
        return active;
    }
}
